﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Hangjing.SQLServerDAL;
using Hangjing.Common;
using Hangjing.Model;
using System.Collections.Generic;

public partial class Admin_AdminDetail : System.Web.UI.Page
{
    EAdmin dal = new EAdmin();

    protected DateTime lastAccess
    {
        set
        {
            ViewState["lastAccess"] = value;
        }
        get
        {
            return ViewState["lastAccess"] == null ? DateTime.Now : Convert.ToDateTime(ViewState["lastAccess"]);
        }
    }

    protected string password
    {
        set
        {
            ViewState["password"] = value;
        }
        get
        {
            return ViewState["password"] == null ? "" : ViewState["password"].ToString();
        }
    }

    protected EAdminInfo info
    {
        set
        {
            this.tbName.Text = value.AdminName;
            WebUtility.SelectValue(stpermition, value.Permission.Trim());
            this.tbRealname.Text = value.RealName;
            lbNotice.InnerHtml = "<font color='red'>不修改密码输入框留空即可.</font>";
            lastAccess = value.LastAccess;
            password = value.AdminPassword;
            WebUtility.SelectValue(ddlRem, value.Rem);
            WebUtility.SelectValue(DDLArea, value.CityID.ToString());

            WebUtility.BindList("id", "classname", SectionProxyData.GetEdelivergroupList().Where(a => a.Status == value.CityID).ToList(), ddlDeliverGroup);




            WebUtility.SelectValue(ddlDeliverGroup, value.AdminStatus);


        }
        get
        {
            EAdminInfo model = new EAdminInfo();
            model.ID = HjNetHelper.GetQueryInt("id", 0);
            model.AdminName = WebUtility.InputText(this.tbName.Text, 20);
            model.RealName = WebUtility.InputText(this.tbRealname.Text, 20);
          
            model.AdminStatus = Convert.ToInt32(ddlDeliverGroup.SelectedValue);
            model.LastAccess = lastAccess;
            model.Permission = stpermition.SelectedValue;
            if (model.Permission.Trim() == "-1")
            {
                model.Rem = "-1";
            }
            else
            {
                model.Rem = ddlRem.SelectedValue;
            }

          

            model.CityID = Convert.ToInt32(DDLArea.SelectedValue);
            if (this.tbPassword.Text != "")
            {
                model.AdminPassword = WebUtility.GetMd5(WebUtility.InputText(this.tbPassword.Text));
            }
            else
            {
                model.AdminPassword = password;
            }
            return model;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        ValidatorSet validator = new ValidatorSet("Admin");
        validator.SetValidator();
        if (!Page.IsPostBack)
        {
            //角色
            IList<sys_RolesInfo> rolses = new sys_Roles().getall("1=1");
            EAdminInfo admin = UserHelp.GetAdmin();
            if (admin.CityID > 0)
            {
                rolses = rolses.Where(a => a.cityid == admin.CityID || a.cityid == 0).ToList();
                //城市管理员不能是超级管理员
                stpermition.Items[1].Enabled = false;


            }

            WebUtility.BindList("RoleID", "R_RoleName", rolses, ddlRem);
            WebUtility.SetDDLCity(DDLArea);
            if (Request["id"] != null)
            {
                getAdminData();
                pageType.Text = "编辑管理员";
            }
            else
            {
                pageType.Text = "添加管理员";
                ddlDeliverGroup.Style["display"] = "none";
            }
        }
    }

    protected void getAdminData()
    {
        EAdminInfo model = null;
        int id = HjNetHelper.GetQueryInt("id", 0);
        model = dal.GetModel(id);
        if (model != null)
        {
            info = model;
           
        }
    }

    protected void btSave_Click(object sender, EventArgs e)
    {
        if (this.tbPassword.Text.Trim() != this.tbConfirm.Text.Trim())
        {
            AlertScript.RegScript(this.Page,UpdatePanel1, "tipsWindown('提示信息','text:密码两次输入不一致!','250','150','true','1000','true','text');typechange();");
            return;
        }
        EAdminInfo model = info;
        if (Request["id"] == null)
        {
            //判断权限
            int _rs = WebUtility.checkOperator(2);
            if (_rs == 0)
            {
                AlertScript.RegScript(this.Page, this.UpdatePanel1, "alert('无操作权限','success','true',5);init();");
                return;
            }


            string sqls = "AdminName='" + info.AdminName + "'  ";
            if (dal.GetCount(sqls) > 0)
            {
                AlertScript.RegScript(this.Page, UpdatePanel1, "alert('相同名称的管理员已经存在','text:新增商家信息失败，登录名已存在!','250','150','true','3000','true','text');init();");
                return;
            }


            int adminid = dal.Add(model);

            if (adminid > 0)
            {
                AlertScript.RegScript(this.Page, UpdatePanel1, "alert('新增成功,点击编辑信息','id:divShowContent','640','150','true','','true','text');gourl('AdminDetail.aspx?id=" + adminid + "');");
            }
            else
            {
                AlertScript.RegScript(this.Page, UpdatePanel1, "tipsWindown('提示信息','text:保存失败!','250','150','true','1000','true','text');;init();");
            }
        }
        else
        {
            //判断权限
            int _rs = WebUtility.checkOperator(3);
            if (_rs == 0)
            {
                AlertScript.RegScript(this.Page, this.UpdatePanel1, "alert('无操作权限','success','true',5);init();");
                return;
            }

            string sqls = "AdminName='" + info.AdminName + "' and id <> " + info.ID;
            if (dal.GetCount(sqls) > 0)
            {
                AlertScript.RegScript(this.Page, UpdatePanel1, "alert('相同名称的管理员已经存在','text:新增商家信息失败，登录名已存在!','250','150','true','3000','true','text');init();");
                return;
            }


            if (dal.Update(model) > 0)
            {
                AlertScript.RegScript(this.Page, UpdatePanel1, "tipsWindown('提示信息','text:保存成功!','250','150','true','1000','true','text');;init();");
            }
            else
            {
                AlertScript.RegScript(this.Page, UpdatePanel1, "tipsWindown('提示信息','text:保存失败!','250','150','true','1000','true','text');;init();");
            }
        }

        SectionProxyData.ClearMarketAdminList();
    }
}
